package comsummer.springboot.jdbc.multiple.repository;

import comsummer.springboot.jdbc.multiple.entity.User;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class UserRepository {

    @Resource
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    public void save(User user) {
        jdbcTemplate.update("insert into user(user_name, borthday) values(?,?)",
                user.getName(),
                user.getBorthday());
    }

    public void deleteById(Long id) {
        jdbcTemplate.update("delete from user where id = ?", id);
    }

    public List<User> getAll() {
        return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
    }

    public User getById(Long id) {
        return jdbcTemplate.queryForObject("select * from user where id = ?", new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
    }

}
